"""
@Project ：Python_Learning 
@File    ：80.删除有序数组中的重复项II.py
@IDE     ：PyCharm 
@Author  ：zhupp
@Date    ：2024/10/20 14:22 
"""
from typing import List


class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        index = 0
        repeat = True
        while index < len(nums) - 1:
            if nums[index] != nums[index + 1]:
                index += 1
                repeat = True
            elif nums[index] == nums[index + 1] and repeat:
                index += 1
                repeat = False
            elif repeat is False and nums[index] == nums[index + 1]:
                nums.pop(index + 1)
        return nums

# class Solution:
#     def removeDuplicates(self, nums: List[int]) -> int:
#         write_index = 1
#         count = 1
#         for read_index in range(1, len(nums)):
#             if nums[read_index] != nums[read_index - 1]:
#                 count = 1
#             else:
#                 count += 1
#
#             if count <= 2:
#                 nums[write_index] = nums[read_index]
#                 write_index += 1
#         return write_index

s = Solution()
print(s.removeDuplicates([1, 1, 1, 2, 2, 2, 3, 3,4]))